package com.zhangxuehai.android.pdfviewer;

import android.graphics.Point;
import android.graphics.RectF;

import com.shockwave.pdfium.util.Size;
import com.shockwave.pdfium.util.SizeF;

/**
 * @author 章学海
 * @date 2019/12/25
 */
public class CoordinateUtils {
    private static final float SIZE = 72.0F;

    /**计算当前view点所在的pdf页面点（pdf坐标）
     * @param pdfView pdf控件
     * @param motionX view坐标系x
     * @param motionY view坐标系y
     * @return {pdf页码,x，y}*/
    public static int[] getPdfCoordinate(PDFView pdfView, float motionX, float motionY){
        //view 缩放系数
        float zoom = pdfView.getZoom();
        //view原点相对于display原点的x位置，基于display坐标
        float xOffset = pdfView.getCurrentXOffset()/zoom;
        //view原点相对于display原点的y位置，基于display坐标
        float yOffset = pdfView.getCurrentYOffset()/zoom;
        //pdf页码每页间距
        int spacingPx = pdfView.getSpacingPx();
        //在display坐标系中，以view原点为原点，点击的点的x的坐标
        float x = motionX/zoom;
        //在display坐标系中，以view原点为原点，点击的点的y的坐标
        float y = motionY/zoom;
        //转换xy，点击点位于display坐标系中的位置
        x = xOffset-x;
        y = yOffset-y;

        int findPage = 0;
        boolean finded = false;
        //顺序查找当前位置所属页面，每次查找如果没有中AOD x | y 平移一页
        while (findPage < pdfView.getPageCount()){
            if(pdfView.isSwipeVertical()){
                float pageHeight = getDisplayPageHeight(pdfView,findPage);
                if(-1*y<pageHeight+spacingPx/2){
                    finded = true;
                    break;
                }
                y += pageHeight;
                y += spacingPx;
            }else{
                float pageWidth = getDisplayPageWidth(pdfView,findPage);
                if(-1*x<pageWidth+spacingPx/2){
                    finded = true;
                    break;
                }
                x += pageWidth;
                x += spacingPx;
            }
            findPage++;
        }
        if(!finded){
            throw new RuntimeException("查找坐标异常");
        }
        //减去当前页的自动边距
        if(pdfView.isAutoSpacingEnabled()){
            if(pdfView.isSwipeVertical()){
                y += (pdfView.getHeight()- pdfView.pdfFile.pageSizes.get(findPage).getHeight())/2;
            }else{
                x += (pdfView.getWidth()- pdfView.pdfFile.pageSizes.get(findPage).getWidth())/2;
            }
        }
        //如果当前不是最大页面,减去页面大小带来的差距
        if(pdfView.isSwipeVertical()){
            if(pdfView.pdfFile.getMaxPageWidth()>pdfView.pdfFile.pageSizes.get(findPage).getWidth()){
                x += (pdfView.pdfFile.getMaxPageWidth()-pdfView.pdfFile.pageSizes.get(findPage).getWidth())/2;
            }
        }else{
            if(pdfView.pdfFile.getMaxPageHeight()>pdfView.pdfFile.pageSizes.get(findPage).getHeight()){
                y += (pdfView.pdfFile.getMaxPageHeight()-pdfView.pdfFile.pageSizes.get(findPage).getHeight())/2;
            }
        }
        float xZoom = pdfView.pdfFile.pageSizes.get(findPage).getWidth()/pdfView.pdfFile.originalPageSizes.get(findPage).getWidth();
        x/=xZoom*-1;
        float yZoom = pdfView.pdfFile.pageSizes.get(findPage).getHeight()/pdfView.pdfFile.originalPageSizes.get(findPage).getHeight();
        y/=yZoom*-1;
        return new int[]{findPage,Math.round(x),Math.round(y)};
    }

    /**把pdf坐标转换为实际pdf坐标*/
    public static SizeF toPdfPointCoordinate(PDFView pdfView,int page ,int x , int y){
        y = pdfView.pdfFile.originalPageSizes.get(page).getHeight() - y;
        return new SizeF(x*SIZE/pdfView.pdfiumCore.mCurrentDpi,y*SIZE/pdfView.pdfiumCore.mCurrentDpi);
    }
    /**把实际pdf坐标转换为pdf坐标*/
    public static Point toPdfPointCoordinateDesc(PDFView pdfView, int page , float x , float y){
        x *= pdfView.pdfiumCore.mCurrentDpi;
        x /= SIZE;
        y *= pdfView.pdfiumCore.mCurrentDpi;
        y /= SIZE;
        y = pdfView.pdfFile.originalPageSizes.get(page).getHeight() - y;
        return new Point((int)x,(int)y);
    }
    /**把实际pdf坐标转换为pdf坐标*/
    public static RectF toPdfPointCoordinateDesc(PDFView pdfView, int page , RectF rectF){
        Point leftTop = toPdfPointCoordinateDesc(pdfView,page,rectF.left,rectF.top);
        Point rightBottom = toPdfPointCoordinateDesc(pdfView,page,rectF.right,rectF.bottom);
        RectF f = new RectF();
        f.left = leftTop.x;
        f.top = leftTop.y;
        f.right = rightBottom.x;
        f.bottom = rightBottom.y;
        return f;
    }
    /**获取当前页面在Display坐标系的宽度（display坐标）*/
    public static float getDisplayPageWidth(PDFView pdfView,int page){
        if(page<0 || page>pdfView.getPageCount()-1){
            return 0F;
        }
        float width = 0F;
        width += pdfView.pdfFile.pageSizes.get(page).getWidth();
        if(pdfView.isAutoSpacingEnabled()){
            width += (pdfView.getWidth()- pdfView.pdfFile.pageSizes.get(page).getWidth());
        }
        return width;
    }
    /**获取当前页面在Display坐标系的高度*/
    public static float getDisplayPageHeight(PDFView pdfView,int page){
        if(page<0 || page>pdfView.getPageCount()-1){
            return 0F;
        }
        float height = 0F;
        height += pdfView.pdfFile.pageSizes.get(page).getHeight();
        if(pdfView.isAutoSpacingEnabled()){
            height += (pdfView.getHeight()- pdfView.pdfFile.pageSizes.get(page).getHeight());
        }
        return height;
    }
    /**获取页码的尺寸（pdf坐标）*/
    public static Size getPdfPageSize(PDFView pdfView,int page){
        return pdfView.pdfFile.originalPageSizes.get(page);
    }

    /**获取页面pdf和display比例*/
    public static float getPdfDisplayRation(PDFView pdfView,int page){
        return pdfView.pdfFile.originalPageSizes.get(page).getWidth()/pdfView.pdfFile.pageSizes.get(page).getWidth();
    }

}
